C.................... FLIP-Select-Ut.for .......................
C.... This program reads a data file containing ASCII data and selects lines of
C.... data to output. Data in select column must be sorted into ascending order.
C.... This program based on the program FLIP-Select.for but is designed to print
C.... the data at selected intervals based on UT. 
C.... Written by P. Richards June 2010. Removed NUMHEAD input 2020-09-02 
      IMPLICIT NONE
      INTEGER I,J                    !.. Year+day for MSIS
      INTEGER COLNUM                 !.. # of columns of data to read
      INTEGER COLSEL                 !.. Column to select on
      INTEGER NUMLINES               !.. # of data lines in the input file
      INTEGER IDIM,DDIM              !.. Array dimensions
      INTEGER COLRAT1,COLRAT2        !.. Columns to ratio
      PARAMETER(IDIM=440000,DDIM=25) !.. Array dimensions
      REAL RDATA(IDIM,DDIM)          !.. Array for data
      REAL UTSTART,UTSTEP            !.. UT starting and step values in COLSEL
      REAL DELLO,DELHI               !.. Used for printing
      REAL RATIO                     !.. Ratio to calculate
      CHARACTER*199 HEADER           !.. For reading the headers
 
      WRITE(6,676)
 676  FORMAT(/6X,'....... FLIP-Select-UT.for 2020-09-02......')

      CALL OPEN_FILE()   !... open files assigned in Compare-ISR-Ti-MSIS-Tn.bat

      !.. Read past the headers to the first control parameter
 5    CONTINUE
        READ(3,*,ERR=5) COLNUM    !.. # of columns of data to read
      READ(3,*) COLSEL            !.. Column to select on
      READ(3,*) UTSTART           !.. Value to select in COLSEL
      READ(3,*) UTSTEP            !.. Value to select in COLSEL
      READ(3,*) COLRAT1, COLRAT2  !.. Columns to ratio

      IF(COLNUM.GT.DDIM) THEN
         WRITE(6,*) ' '
         WRITE(6,*) ' ERROR..ERROR..ERROR..ERROR..ERROR..ERROR..'
         WRITE(6,*) ' Too many columns selected, limit=',DDIM
      STOP
      ENDIF

      IF(COLRAT1.GT.COLNUM.OR.COLRAT2.GT.COLNUM) THEN
         WRITE(6,*) ' '
         WRITE(6,*) ' ERROR..ERROR..ERROR..ERROR..ERROR..ERROR..'
         WRITE(6,*) ' COLRAT1 or COLRAT2 is greater than COLNUM'
      STOP
      ENDIF
    
      J=1
      !.. read data into the array
 10   CONTINUE
        READ(1,*,ERR=10,END=30) (RDATA(J,I),I=1,COLNUM)
        J=J+1
        IF(J.GT.IDIM) THEN
          WRITE(6,'(I9,3F9.2)') J,(RDATA(NUMLINES,I),I=1,3)
          STOP
        ENDIF
      GOTO 10

 30   CONTINUE
      NUMLINES=J-1
      WRITE(6,'(A,I9)') '  # of lines in input file =',NUMLINES

      !.. Loop to read the rest of the input data
      DO J=2,NUMLINES-1
         !..  Output line if it satisfies the selected value
         IF(RDATA(J+1,COLSEL).GE.UTSTART.AND.RDATA(J,COLSEL).LT.UTSTART)
     >   THEN
            DELLO=ABS(RDATA(J,COLSEL)-UTSTART)
            DELHI=ABS(RDATA(J+1,COLSEL)-UTSTART)
            RATIO=0.00
            IF(COLRAT1.LE.COLNUM.AND.COLRAT2.LE.COLNUM) THEN
              IF(RDATA(J,COLRAT2).GT.0) RATIO=RDATA(J,COLRAT1)/
     >            RDATA(J,COLRAT2)
            ENDIF
            IF(DELLO.LT.DELHI) THEN
               WRITE(4,191) (RDATA(J,I),I=1,COLNUM),RATIO    !.. closest to pt. J
            ELSE
               WRITE(4,191) (RDATA(J+1,I),I=1,COLNUM),RATIO  !.. closest to pt. J+1
            ENDIF    
            UTSTART=UTSTART+UTSTEP
          ENDIF
      ENDDO

 90   CONTINUE
      STOP
 91   CONTINUE
      WRITE(6,190) 
 190  FORMAT(//'  *** ERROR: Check the number of input file headers'//)
 191  FORMAT(F11.2,1P,99E11.3)
      STOP
      END
